/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cms.action;

import com.opensymphony.xwork2.ActionSupport;
import dto.Profile;
import dto.User;
import java.util.ArrayList;

import java.util.List;
/**
 *
 * @author Gary
 */
public class UserAction extends ActionSupport{
    
    private static final long serialVersionUID = 1L;
    List<User> users;
    private User user;
    private String userName;
    private boolean isUpdate;

    public boolean isIsUpdate() {
        return isUpdate;
    }

    public void setIsUpdate(boolean isUpdate) {
        this.isUpdate = isUpdate;
    }
    private Profile profile;

    public Profile getProfile() {
        return profile;
    }

    public void setProfile(Profile profile) {
        this.profile = profile;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
   
   public String list() throws Exception {
        users = new ArrayList<User>();
        for (int i = 0; i < 10; i++) {
           User u = new User("Bobo" + i, "pass", i/2==1?"admin":"participant");
           boolean add = users.add(u);
       }
	return "userlist";
    }
    public String edit() throws Exception {
                // dummy data
                //user = new User(userName, "");
		if (user != null) {
                    isUpdate = true;
		}
                // get user profile
                //dummy data
                profile = new Profile(userName, "blog", "www.website.com", "ABC Company", "ABC Street", "60000000", "mail@mail.com", "60000000", "9000000", "Home Street", "733685");
		return "userform";
	}

    public String create() throws Exception {
		isUpdate = false;
		user = new User(userName, "");
                profile = null;
		return "userform";
	}

    public String update() throws Exception {
		if (isUpdate) {
			// update conference
		} else {
                        // insert conference
		}
		return "userupdated";
	}

    public String delete() throws Exception {
		return "userupdated";
	}

    public void validate() {
		if (user != null) {
			if (user.getUserName().trim().length() == 0) {
                                addFieldError("user.userName", "User Name is not required.");
			}
		}
	}
    private User findUserByName(String userName) {
		return null;
	}
}
